<template>
    <div class="login_contaniner">
        <div class="login_box">
            <!-- 头像区域 -->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 登陆区域 -->
            <el-form ref="loginFormRef" :model="loginForm" :rules="rules" label-width="0px" class="
            login_form">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <!-- 用户密码 -->
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="el-icon-star-on" type="password"></el-input>
                </el-form-item>

                <!-- 登录按钮 -->
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登陆</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>

</template>

<script>
    export default {
        data() {
            return {
                //这登陆的数据表单的绑定对象
                loginForm: {
                    username: '123',
                    password: '456'
                },

                rules: {
                    //验证登录名
                    username: [{
                            required: true,
                            message: '请输入登陆用户名',
                            trigger: 'blur'
                        },
                        {
                            min: 5,
                            max: 10,
                            message: '长度在 6 到 10 个字符',
                            trigger: 'blur'
                        }
                    ],
                    password: [{
                            required: true,
                            message: '请输入用户密码',
                            trigger: 'blur'
                        },
                        {
                            min: 5,
                            max: 12,
                            message: '长度在 6 到 12 个字符',
                            trigger: 'blur'
                        }
                    ]
                },
            }
        },
        methods: {
            resetLoginForm: function () {
                // console.log(this);
                this.$refs.loginFormRef.resetFields();
            },
            login() {
                this.$refs.loginFormRef.validate(async valid => {
                    if (!valid) return;
                    const {
                        data: res
                    } = await this.$http.post("login", this.loginForm);
                    if (res.meta.status !== 200) {
                        this.$message.error('登陆失败')
                    } else {
                        this.$message.success('登陆成功') 
                 window.sessionStorage.setItem("token", res.data.token);
                this.$router.push("/Home")
                    }
                }
                
                );
              


                //
            }
        }
    };
</script>

<style lang="less" scoped>
    .login_contaniner {
        background-color: rgb(21, 66, 105);
        height: 100%;
    }

    .login_box {
        width: 445px;
        height: 300px;
        background-color: rgb(102, 63, 63);
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);

        .avatar_box {
            height: 130px;
            width: 130px;
            border: 1px solid rgb(165, 125, 125);
            border-radius: 60%;
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #fff;

            img {
                width: 100%;
                height: 100%;
                border-radius: 60%;

            }

        }

    }

    .login_form {
        position: absolute;
        bottom: 0%;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }
</style>